package com.it.flowable.util;

import org.flowable.bpmn.model.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Utils {


    public static Event createStartEvent(String id, String name, Map<String, List<ExtensionAttribute>> attributes, List<FlowableListener> executionListeners, List<FormProperty> formProperties, Map<String, ? extends List<ExtensionElement>> extensionElements) {
        StartEvent event = new StartEvent();
        event.setId(id);
        event.setName(name);
        event.setInitiator("initiator");

        if (attributes != null) {
            event.getAttributes().putAll(attributes); // 属性覆盖
        }
        if (executionListeners != null) {
            event.getExecutionListeners().addAll(executionListeners);
        }
        if (formProperties != null) {
            event.getFormProperties().addAll(formProperties);
        }
        if (extensionElements != null) {
            event.getExtensionElements().putAll(extensionElements);
        }
        // event.setFormKey("${formKey}");
        // event.setInterrupting(true);
        // event.setValidateFormFields();
        // event.setSameDeployment(true);

        // event.setIncomingFlows(null);
        // event.setOutgoingFlows(null);

        // event.setInParameters(null);
        // event.setOutParameters(null);
        return event;
    }

    public static Event createEndEvent(String id, String name) {
        Event event = new EndEvent();
        event.setId(id);
        event.setName(name);
        return event;
    }
    /**
     * 创建通用用户任务
     */
    public static UserTask createUserTask(String id, String name, String assignee, String category, List<FormProperty> formProperties, Collection<? extends FlowableListener> executionListeners, Collection<? extends FlowableListener> taskListeners) {
        UserTask task = new UserTask();
        task.setId(id);
        task.setName(name);
        task.setAssignee(assignee);
        task.setCategory(category);
        if (formProperties != null) {
            task.getFormProperties().addAll(formProperties);
        }
        if (taskListeners != null) {
            task.getTaskListeners().addAll(taskListeners);
        }
        if (executionListeners != null) {
            task.getExecutionListeners().addAll(executionListeners);
        }
        return task;
    }

    public static ServiceTask createServiceTask(String id, String name, String javaDelegate, Collection<? extends FieldExtension> fieldExtensions) {
        ServiceTask task = new ServiceTask();
        task.setId(id);
        task.setName(name);
        ListenerUtils.addJavaDelegate(task, javaDelegate);
        if (fieldExtensions != null) {
            task.getFieldExtensions().addAll(fieldExtensions);
        }
        // task.getMapExceptions();
        // task.getExecutionListeners();
        // task.getAttributes();
        // task.setResultVariableName(resultVariable);

        return task;
    }


    /**
     * 创建普通序列流
     */
    public static SequenceFlow createSequenceFlow(String source, String target, String name, String conditionExpression, Collection<? extends FlowableListener> flowableListeners) {
        SequenceFlow flow = new SequenceFlow(source, target);
        if (name != null) {
            flow.setName(name);
        }
        if (conditionExpression != null) {
            flow.setConditionExpression(conditionExpression);
        }
        if (flowableListeners != null) {
            flow.getExecutionListeners().addAll(flowableListeners);
        }
        return flow;
    }

    /**
     * 创建排他网关
     */
    public static ExclusiveGateway createExclusiveGateway(String id, String name) {
        ExclusiveGateway gateway = new ExclusiveGateway();
        gateway.setId(id);
        gateway.setName(name);
        return gateway;
    }

}
